<?

// Check to ensure we are not using webcp-httpd as net2ftp is not compatible
if ($cfg['httpd_mode'] != 'webcp') {

// Buffer net2ftp for HTML corrections
ob_start();

// Change to the net2ftp directory to allow includes to work properly
chdir('personal/net2ftp');

// Obtain real username
$uniquedata['username'] = $personaldata['username'];
$uniquedata['id'] = $personaldata['id'];
call_hook('user:get:username', $uniquedata);
$unique = $uniquedata['username'];

// Check for first visit and set variables

if (!isset($_POST['state']) && !isset($state)) { $_POST['state'] = 'browse'; }
if (!isset($_POST['state2']) && !isset($state2)) { $_POST['state2'] = 'main'; }
if (!isset($_POST['input_ftpserver']) && !isset($_POST['net2ftp_ftpserver']) && !isset($ftpserver)) { $_POST['input_ftpserver'] = $cfg['ftpsysname']; }
if (!isset($_POST['input_ftpserverport']) && !isset($_POST['net2ftp_ftpserverport']) && !isset($ftpserverport)) { $_POST['input_ftpserverport'] = $cfg['ftpsysport']; }
if (!isset($_POST['input_username']) && !isset($_POST['net2ftp_username']) && !isset($username)) { $_POST['input_username'] = $unique; }
if (!isset($_POST['input_password']) && !isset($_POST['net2ftp_password']) && !isset($password_encrypted)) { $_POST['input_password'] = $personaldata['password']; }

if (!isset($_POST['input_ftpmode']) && !isset($_POST['net2ftp_ftpmode']) && !isset($ftpmode)) { $_POST['input_ftpmode'] = 'binary'; }
if (!isset($_POST['input_directory']) && !isset($_POST['net2ftp_directory']) && !isset($directory)) { $_POST['input_directory'] = '/'; }
if (!isset($_POST['input_skin']) && !isset($_POST['net2ftp_skin'])) { $_POST['input_skin'] = '2'; }
if (!isset($_POST['cookiesetonlogin']) && !isset($_POST['net2ftp_ftpserver']) && !isset($ftpserver)) { $_POST['cookiesetonlogin'] = 'yes'; }

if (!isset($_POST['input_language']) && !isset($_POST['net2ftp_language']) && !isset($language)) {

	if (isset($lang)) {
		$lang = str_replace("/","",$lang);
		switch($lang) {
			case "english":
				$_POST['input_language'] = 'en';
				break;
			case "danish":
				$_POST['input_language'] = 'en';
				break;
			case "french":
				$_POST['input_language'] = 'fr';
				break;
			case "german":
				$_POST['input_language'] = 'de';
				break;
			case "italian":
				$_POST['input_language'] = 'it';
				break;
			case "norwegian":
				$_POST['input_language'] = 'en';
				break;
			case "russian":
				$_POST['input_language'] = 'ru';
				break;
			case "spanish":
				$_POST['input_language'] = 'es';
				break;	
			case "swedish":
				$_POST['input_language'] = 'en';
				break;			
		}
	}
}

// Include the main net2ftp file
include('index.php');

// Get net2ftp output and clean buffer
$output = ob_get_contents();

ob_end_clean();

if ($state2 == 'viewimage') {
	echo $output;
} else {
// Don't search/replace in code of files being viewed or edited
if (($_POST['state2'] == 'view' || $_POST['state2'] == 'edit') && $codeStart = strstr($output, "<!-- -------------------- Start of code -------------------- -->")) {
	$codeEnd = strstr($output, "<!-- -------------------- End  of code  -------------------- -->") + strlen("<!-- -------------------- End  of code  -------------------- -->");
	$code = substr($output, $codeStart, $codeEnd - $codeStart);
	$codeAbove = substr($output, 0, $codeStart);
	$codeBelow = substr($output, $codeEnd, strlen($output) - $codeEnd);
} else {
	$codeAbove = $output;
	$code = '';
	$codeBelow = '';
}

// Redirect all links/forms to the current web-cp page
$search[] = '/\=\"(\/)?index.php/';
$replace[] = '="'.$current_url;
$search[] = '/(\.open\(|href\=)(\\\'|\")([a-zA-Z0-9\_\-]*)(.html|.htm|.txt|.css)(\\\'|\")/';
$replace[] = "$1$2personal/net2ftp/$3$4$5";
$search[] = '/\=\"(\/)?jupload.php/';
$replace[] = '="personal/net2ftp/jupload.php';

// Remove Open functionality
$search[] = '/\<td onClick=\'window.open\((.*)Open\<\/td\>/';
$replace[] = '';

// Change the image and plugin source to reflect the correct directory
$search[] = '/images\//';
$replace[] = 'personal/net2ftp/images/';
$search[] = '/plugins\//';
$replace[] = 'personal/net2ftp/plugins/';

// Strip header/footer info that is not needed
$search[] = '/\<\!DOC(.*)\<link/s';
$replace[] = '<link';
$search[] = '/<\/head\>(.*)\<body(| onload\=\"(.*)\;\")\>/s';
$replace[] = '';
$search[] = '/\<\/body\>/';
$replace[] = '';
$search[] = '/\<\/html\>/';
$replace[] = '';

// Escape skin for viewing images
$search[] = '/state2\=viewimage/';
$replace[] = 'state2=viewimage&noskin=1';
$search[] = '/\?ftpserver\=/';
$replace[] = '&ftpserver=';

// Escape skin for downloading files
$search[] = '/function submitBrowseForm\(directory\, entry\, state\, state2\) \{/';
$replace[] = 'function submitBrowseForm(directory, entry, state, state2) {'."\r\n".'	if (state2 == "downloadfile" || state2 == "download") { document.BrowseForm.noskin.value=1; } else { document.BrowseForm.noskin.value=0; }';
$search[] = '/\<input type\=\"hidden\" name\=\"entry\" \/\>/';
$replace[] = '<input type="hidden" name="entry" />'."\r\n".'<input type="hidden" name="noskin" value="0" />';

// Escape skin for popups
$search[] = '/d\.writeln\(\\\'\<input type\=\"hidden\" name\=\"state2\" value=\"popup\" \/\>\\\'\)\;/';
$replace[] = 'd.writeln(\'<input type="hidden" name="state2" value="popup">\');'."\r\n".'d.writeln(\'<input type="hidden" name="noskin" value="1">\');';
$search[] = '/\<input type\=\"hidden\" name\=\"state2\" value\=\"popup\" \/\>/';
$replace[] = '<input type="hidden" name="state2" value="popup" />'."\r\n".'<input type="hidden" name="noskin" value="1" />';

// Make changes and echo
$output = preg_replace($search, $replace, $codeAbove).$code.preg_replace($search, $replace, $codeBelow);
if ($state2 != 'popup') {
?>

<div align="center" style='margin-left: 5px;'>
<table border="0" cellpadding="0" cellspacing="0" width=800><tr><td align=center>
<table border="0" cellpadding="0" cellspacing="1" class="tblbg"><tr><td align=center>
<table border="0" cellspacing="1" class="content">
                        <tr>
                                <td class="tbltop">&nbsp;<?=$T['menu'][$url];?>&nbsp;</td>
                        </tr>
                        <tr>
<td>
<? }

	echo $output; 

if ($state2 != 'popup') {
?>
</td></tr></table>
</td></tr></table>
</td></tr></table>
<?
}
}
} else {
?>
<div align=center>
Sorry, net2ftp is not compatible with your configuration.  You may not use net2ftp with webcp-httpd.
</div>
<? } ?>

